<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\admin\command\Project;

use app\admin\model\AuthRule;
use app\admin\model\VipExpires;
use app\common\model\Order;
use app\common\model\Team;
use app\common\model\User;
use ReflectionClass;
use ReflectionMethod;
use think\Cache;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use think\Loader;

class ExpireVip extends Command
{
    protected $model = null;

    protected function configure()
    {
        $this->setName('expireVip')
            ->setDescription('expire order deal with');
        //要执行的controller必须一样，不适用模糊查询
    }

    protected function execute(Input $input, Output $output)
    {
        $page = 1;
        $step = 100;
        try {
            $VipExpiresModel = new VipExpires();
            while (true){
                $start = ($page-1)*$step;
                $info = $VipExpiresModel->where('end_date','<',time())->limit($start,$step)->select();
                if (!$info){
                    exit();
                }
                foreach ($info as $k=>$v){
                    Db::startTrans();
                    try {
                        $VipExpiresModel->where('id',$v->id)->delete();
                        $vipUser = \app\common\model\VipExpires::where('user_id',$v->user_id)->order('level_sort')->find();
                        if(!User::where('id',$v->user_id)->update([
                            'temp_level'=>$vipUser['level'] ?? 0,
                            'updatetime'=>time(),
                        ])){
                            throw new Exception('用户信息更新失败');
                        }
                        Db::commit();
                    }catch (Exception $e){
                        Db::rollback();
                    }

                }

                $page++;
            }
        }catch (Exception $exception){
            print_r($exception->getMessage());
        }
    }

}
